package com.information.manage.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.information.manage.basic.common.token.TokenUtils;
import com.information.manage.basic.mapper.*;
import com.information.manage.basic.model.entity.SysMenuDO;
import com.information.manage.basic.model.entity.SysRelRoleMenuDO;
import com.information.manage.basic.model.entity.SysRelUserMenuDO;
import com.information.manage.basic.model.entity.SysRoleDO;
import com.information.manage.basic.model.query.AddMenuQuery;
import com.information.manage.basic.model.query.UpdateMenuQuery;
import com.information.manage.basic.common.recycling.RecoveryDataOperateManager;
import com.information.manage.basic.common.result.Result;
import com.information.manage.basic.service.SysMenuService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenuDO> implements SysMenuService {

    @Resource
    private SysMenuMapper sysMenuMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysRelRoleMenuMapper sysRelRoleMenuMapper;

    @Resource
    private SysRelUserMenuMapper sysRelUserMenuMapper;

    @Resource
    private RecoveryDataOperateManager recoveryDataOperateManager;

    @Override
    public Result<Integer> addMenu(HttpServletRequest request,AddMenuQuery addMenuQuery) {
        Integer parentId = -1;
        if (addMenuQuery.getMenuParentId() == null) addMenuQuery.setMenuParentId(parentId);
        SysMenuDO sysMenuDO = new SysMenuDO();
        sysMenuDO.setName(addMenuQuery.getMenuName());
        sysMenuDO.setDescription(addMenuQuery.getDescription() == null ? null : addMenuQuery.getDescription());
        sysMenuDO.setParentId(addMenuQuery.getMenuParentId());
        sysMenuDO.setCreateTime(LocalDateTime.now());
        sysMenuDO.setMust(addMenuQuery.isMust());
        sysMenuDO.setType(addMenuQuery.getMenuType());
        sysMenuDO.setHide(addMenuQuery.isHide());
        sysMenuDO.setRoute(addMenuQuery.getRoute());
        sysMenuDO.setIcon(addMenuQuery.getIcon());
        sysMenuMapper.insert(sysMenuDO);

        Integer userId = TokenUtils.getUserId(StringUtils.isEmpty(request.getHeader("token")) ? request.getAttribute("token").toString() : request.getHeader("token"));
        LambdaQueryWrapper<SysRoleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleDO::getName, sysUserMapper.selectById(userId).getName());
        Integer userRoleId = sysRoleMapper.selectOne(queryWrapper).getId();
        SysRelRoleMenuDO sysRelRoleMenuDO = new SysRelRoleMenuDO();
        sysRelRoleMenuDO.setRoleId(userRoleId);
        sysRelRoleMenuDO.setMenuId(sysMenuDO.getId());
        sysRelRoleMenuMapper.insert(sysRelRoleMenuDO);

        SysRelUserMenuDO sysRelUserMenuDO = new SysRelUserMenuDO();
        sysRelUserMenuDO.setUserId(userId);
        sysRelUserMenuDO.setMenuId(sysMenuDO.getId());
        sysRelUserMenuDO.setMenuParentId(addMenuQuery.getMenuParentId());
        sysRelUserMenuDO.setSequence(1);
        sysRelUserMenuMapper.insert(sysRelUserMenuDO);
        return Result.ok(sysMenuDO.getId());
    }

    @Override
    public Boolean deleteMenu(List<Integer> menuIds) {
        recoveryDataOperateManager.deleteData(SysMenuDO.class, menuIds);
        return true;
    }

    @Override
    public Result<Boolean> updateMenu(UpdateMenuQuery updateMenuQuery) {
        SysMenuDO sysMenuDO = new SysMenuDO();
        sysMenuDO.setId(updateMenuQuery.getMenuId());
        sysMenuDO.setName(updateMenuQuery.getName() == null || "".equals(updateMenuQuery.getName().trim()) ? null : updateMenuQuery.getName());
        sysMenuDO.setDescription(updateMenuQuery.getDescription() == null || "".equals(updateMenuQuery.getDescription().trim()) ? null : updateMenuQuery.getDescription());
        sysMenuDO.setMust(updateMenuQuery.isMust());
        sysMenuDO.setUpdateTime(LocalDateTime.now());
        sysMenuMapper.updateById(sysMenuDO);
        return Result.ok(true);
    }

}
